function pwdLevel(value) {
	var pattern_1 = /^.*([\W_])+.*$/i;
	var pattern_2 = /^.*([a-zA-Z])+.*$/i;
	var pattern_3 = /^.*([0-9])+.*$/i;
	var level = 0;

	if (value.length > 10) {
		level++;
	}

	if (pattern_1.test(value)) {
		level++;
	}

	if (pattern_2.test(value)) {
		level++;
	}

	if (pattern_3.test(value)) {
		level++;
	}

	if (level > 3) {
		level = 3;
	}

	return level;
}

var validateRegExp = {
	decmal : "^([+-]?)\\d*\\.\\d+$",
	decmal1 : "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$",
	decmal2 : "^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$",
	decmal3 : "^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$",
	decmal4 : "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$",
	decmal5 : "^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$",
	intege : "^-?[1-9]\\d*$",
	intege1 : "^[1-9]\\d*$",
	intege2 : "^-[1-9]\\d*$",
	num : "^([+-]?)\\d*\\.?\\d+$",
	num1 : "^[1-9]\\d*|0$",
	num2 : "^-[1-9]\\d*|0$",
	ascii : "^[\\x00-\\xFF]+$",
	chinese : "^[\\u4e00-\\u9fa5]+$",
	color : "^[a-fA-F0-9]{6}$",
	date : "^\\d{4}(\\-|\\/|\.)\\d{1,2}\\1\\d{1,2}$",
	email : "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$",
	idcard : "^[1-9]([0-9]{14}|[0-9]{17})$",
	ip4 : "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$",
	letter : "^[A-Za-z]+$",
	letter_l : "^[a-z]+$",
	letter_u : "^[A-Z]+$",
	mobile : "0?(13|15)[0-9]{9}$",
	notempty : "^\\S+$",
	password : "^[A-Za-z0-9_-]+$",
	picture : "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$",
	qq : "^[1-9]*[1-9][0-9]*$",
	rar : "(.*)\\.(rar|zip|7zip|tgz)$",
	url : "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$",
	username : "^[A-Za-z0-9_\\-\\u4e00-\\u9fa5]+$",
	zipcode : "^\\d{6}$",
	realname : "^[A-Za-z0-9\\u4e00-\\u9fa5]+$",
	companysite : "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&#=]*)?$"
};

(function($) {
	$.fn.Validate = function(option, callback, def) {
		var ele = this;
		var id = ele.attr("id");
		var type = ele.attr("type");
		var rel = ele.attr("rel");
		var _onFocus = $("#" + id + validateSettings.onFocus.container);
		var _succeed = $("#" + id + validateSettings.succeed.container);
		var _isNull = $("#" + id + validateSettings.isNull.container);
		var _error = $("#" + id + validateSettings.error.container);
		if (def == true) {
			var str = ele.val();
			var tag = ele.attr("sta");
			if (str == "" || str == "-1") {
				validateSettings.isNull.run({
					prompts : option,
					element : ele,
					isNullEle : _isNull,
					succeedEle : _succeed
				}, option.isNull);
			} else if (tag == 1 || tag == 2) {
				return;
			} else {
				callback({
					prompts : option,
					element : ele,
					value : str,
					errorEle : _error,
					succeedEle : _succeed
				});
			}
		} else {
			if (typeof def == "string") {
				ele.val(def);
			}
			if (type == "checkbox" || type == "radio") {
				if (ele.attr("checked") == true) {
					ele.attr("sta", validateSettings.succeed.state);
				}
			}
			switch (type) {
			case "text":
			case "password":
				ele.bind("focus", function() {
					var str = ele.val();
					if (str == def) {
						ele.val("");
					}
					validateSettings.onFocus.run({
						prompts : option,
						element : ele,
						value : str,
						onFocusEle : _onFocus,
						succeedEle : _succeed
					}, option.onFocus);
				}).bind("blur", function() {
					var str = ele.val();
					if (str == "") {
						ele.val(def);
					}
					if (validateRules.isNull(str)) {
						validateSettings.isNull.run({
							prompts : option,
							element : ele,
							value : str,
							isNullEle : _isNull,
							succeedEle : _succeed
						}, "");
					} else {
						callback({
							prompts : option,
							element : ele,
							value : str,
							errorEle : _error,
							isNullEle : _isNull,
							succeedEle : _succeed
						});
					}
				});
				break;
			default:
				if (rel && rel == "select") {
					ele.bind("change", function() {
						var str = ele.val();
						callback({
							prompts : option,
							element : ele,
							value : str,
							errorEle : _error,
							isNullEle : _isNull,
							succeedEle : _succeed
						});
					})
				} else {
					ele.bind("click", function() {
						callback({
							prompts : option,
							element : ele,
							errorEle : _error,
							isNullEle : _isNull,
							succeedEle : _succeed
						});
					})
				}
				break;
			}
		}
	}
})(jQuery);

var validateSettings = {
	onFocus : {
		state : null,
		container : "_error",
		style : "focus",
		run : function(option, str) {
			if (!validateRules.checkType(option.element)) {
				option.element.removeClass(validateSettings.INPUT_style2)
						.addClass(validateSettings.INPUT_style1);
			}
			option.onFocusEle.removeClass().addClass(
					validateSettings.onFocus.style).html(str);
		}
	},
	isNull : {
		state : 0,
		container : "_error",
		style : "null",
		run : function(option, str) {
			option.element.attr("sta", 0);
			if (!validateRules.checkType(option.element)) {
				if (str != "") {
					option.element.removeClass(validateSettings.INPUT_style1)
							.addClass(validateSettings.INPUT_style2);
				} else {
					option.element.removeClass(validateSettings.INPUT_style2)
							.removeClass(validateSettings.INPUT_style1);
				}
			}
			option.succeedEle.removeClass(validateSettings.succeed.style);
			option.isNullEle.removeClass().addClass(
					validateSettings.isNull.style).html(str);
		}
	},
	error : {
		state : 1,
		container : "_error",
		style : "error",
		run : function(option, str) {
			option.element.attr("sta", 1);
			if (!validateRules.checkType(option.element)) {
				option.element.removeClass(validateSettings.INPUT_style1)
						.addClass(validateSettings.INPUT_style2);
			}
			option.succeedEle.removeClass(validateSettings.succeed.style);
			option.errorEle.removeClass()
					.addClass(validateSettings.error.style).html(str);
		}
	},
	succeed : {
		state : 2,
		container : "_succeed",
		style : "succeed",
		run : function(option) {
			option.element.attr("sta", 2);
			option.errorEle.empty();
			if (!validateRules.checkType(option.element)) {
				option.element.removeClass(validateSettings.INPUT_style1)
						.removeClass(validateSettings.INPUT_style2);
			}
			if (option.element.attr("id") == "schoolinput"
					&& $("#schoolid").val() == "") {
				return;
			}
			option.succeedEle.addClass(validateSettings.succeed.style);
		}
	},
	INPUT_style1 : "highlight1",
	INPUT_style2 : "highlight2"

};

var validateRules = {
	isNull : function(str) {
		return (str == "" || typeof str != "string");
	},
	betweenLength : function(str, _min, _max) {
		return (str.length >= _min && str.length <= _max);
	},
	isUid : function(str) {
		return new RegExp(validateRegExp.username).test(str);
	},
	isPwd : function(str) {
		return new RegExp(validateRegExp.password).test(str);
	},
	isPwd2 : function(str1, str2) {
		return (str1 == str2);
	},
	isEmail : function(str) {
		return new RegExp(validateRegExp.email).test(str);
	},
	isTel : function(str) {
		return new RegExp(validateRegExp.tel).test(str);
	},
	isMobile : function(str) {
		return new RegExp(validateRegExp.mobile).test(str);
	},
	checkType : function(element) {
		return (element.attr("type") == "checkbox"
				|| element.attr("type") == "radio" || element.attr("rel") == "select");
	},
	isChinese : function(str) {
		return new RegExp(validateRegExp.chinese).test(str);
	},
	isRealName : function(str) {
		return new RegExp(validateRegExp.realname).test(str);
	},
	isDeptname : function(str) {
		return new RegExp(validateRegExp.deptname).test(str);
	},
	isCompanyname : function(str) {
		return new RegExp(validateRegExp.companyname).test(str);
	},
	isCompanyaddr : function(str) {
		return new RegExp(validateRegExp.companyaddr).test(str);
	},
	isCompanysite : function(str) {
		return new RegExp(validateRegExp.companysite).test(str);
	},
	simplePwd : function(str) {
		return pwdLevel(str) == 1;
	}
};

var validatePrompt = {
	username : {
		onFocus : "用户名的长度为4-20个字符",
		succeed : "",
		isNull : "请输入您的用户名！",
		error : {
			beUsed : "用户名已被占用，请重新输入。",
			badLength : "用户名必须为4-20个字符！",
			badFormat : "用户名仅支持字母，数字，下划线(_)和连字符(-)。"
		}
	},
	pwd : {
		onFocus : "密码的长度为6-16个字符。",
		succeed : "",
		isNull : "请输入密码！",
		error : {
			badLength : "密码的长度为6-16个字符",
			badFormat : "密码仅支持字母，数字，下划线(_)和连字符(-)。",
			simplePwd : "你的密码安全性不够。"
		}
	},
	pwd2 : {
		onFocus : "请输入您的确认密码。",
		succeed : "",
		isNull : "请重新输入您的确认密码！",
		error : {
			badLength : "密码的长度为6-16个字符.",
			badFormat2 : "您的两次输入的密码不同！.",
			badFormat1 : "密码仅支持字母，数字，下划线(_)和连字符(-)."
		}
	},
	mail : {
		onFocus : "E-mail的长度为50个字符以内。",
		succeed : "",
		isNull : "请输入您的E-Mail",
		error : {
			beUsed : "对不起，您输入的邮箱已被注册！",
			beYahoo : "由于雅虎邮箱的服务器已经停止，请重新输入别的邮箱！",
			badFormat : "请输入正确格式的邮箱地址！",
			badLength : "The Email的长度必须在50个字符以内！"
		}
	},
	authcode : {
		onFocus : "请输入验证码！",
		succeed : "",
		isNull : "请输入图片中的验证码！",
		error : "验证码错误或已过期！"
	},
	protocol : {
		onFocus : "",
		succeed : "",
		isNull : "Please certify that you have read and agree to the 360buy.com Terms of Service.",
		error : ""
	},
	referrer : {
		onFocus : "If you sign up and finish orders, your inviter maybe get a chance to be rewarded.",
		succeed : "",
		isNull : "",
		error : ""
	},
	schoolinput : {
		onFocus : "",
		succeed : "",
		isNull : "",
		error : ""
	},
	empty : {
		onFocus : "",
		succeed : "",
		isNull : "",
		error : ""
	},
	loginname : {
		onFocus : "请输入用户名或邮箱",
		succeed : "",
		isNull : "账号不能为空！",
		error : {
			badLength : "错误的账号长度！",
		}
	}
};

var nameold, emailold, authcodeold;
var namestate = false, emailstate = false, authcodestate = false;

var validateFunction = {
	username : function(option) {
		var format = validateRules.isUid(option.value);
		var length = validateRules.betweenLength(option.value.replace(
				/[^\x00-\xff]/g, "**"), 4, 20);
		if (!length && format) {
			validateSettings.error.run(option, option.prompts.error.badLength);
		} else if (!length && !format) {
			validateSettings.error.run(option, option.prompts.error.badFormat);
		} else if (length && !format) {
			validateSettings.error.run(option, option.prompts.error.badFormat);
		} else {
			if (!namestate || nameold != option.value) {
				if (nameold != option.value) {
					nameold = option.value;
					option.errorEle
							.html("<span style='color:#999'>验证中...</span>");
					$.getJSON("../user/isUsernameEngaged.action?username=" + escape(option.value)
							+ "&r=" + Math.random(), function(date) {
						if (date.success == "SUCCESS") {
							validateSettings.succeed.run(option);
							namestate = true;
						} else {
							validateSettings.error.run(option,
									option.prompts.error.beUsed.replace("{1}",
											option.value));
							namestate = false;
						}
					})
				} else {
					validateSettings.error.run(option,
							option.prompts.error.beUsed.replace("{1}",
									option.value));
					namestate = false;
				}
			} else {
				validateSettings.succeed.run(option);
			}
		}
	},
	pwd : function(option) {
		var str1 = option.value;
		var str2 = $("#pwd2").val();
		var format = validateRules.isPwd(option.value);
		var length = validateRules.betweenLength(option.value, 6, 16);
		$("#pwdstrength").hide();
		if (!length && format) {
			validateSettings.error.run(option, option.prompts.error.badLength);
		} else if (!length && !format) {
			validateSettings.error.run(option, option.prompts.error.badFormat);
		} else if (length && !format) {
			validateSettings.error.run(option, option.prompts.error.badFormat);
		} else if (validateRules.simplePwd(str1)) {
			validateSettings.error.run(option, option.prompts.error.simplePwd);
		} else {
			validateSettings.succeed.run(option);
			validateFunction.pwdstrength();
		}
		if (str2 != "") {
			$("#pwd2").jdValidate(validatePrompt.pwd2, validateFunction.pwd2,
					true);
		}
	},
	loginname : function(option) {
		var str1 = option.value;
		var isNull = validateRules.isNull(option.value);
		var length = validateRules.betweenLength(option.value, 4, 50);
		if (!length && !isNull) {
			validateSettings.error.run(option, option.prompts.error.badLength);
		} else if (length && isNull) {
			validateSettings.error.run(option, option.prompts.isNull);
		} else {
			validateSettings.succeed.run(option);
		}
	},
	pwd2 : function(option) {
		var str1 = option.value;
		var str2 = $("#pwd").val();
		var length = validateRules.betweenLength(option.value, 6, 16);
		var format2 = validateRules.isPwd2(str1, str2);
		var format1 = validateRules.isPwd(str1);
		if (!length) {
			validateSettings.error.run(option, option.prompts.error.badLength);
		} else {
			if (!format1) {
				validateSettings.error.run(option,
						option.prompts.error.badFormat1);
			} else {
				if (!format2) {
					validateSettings.error.run(option,
							option.prompts.error.badFormat2);
				} else {
					validateSettings.succeed.run(option);
				}
			}
		}
	},
	mail : function(option) {
		var format = validateRules.isEmail(option.value);
		var format2 = validateRules.betweenLength(option.value, 0, 50);
		if (!format) {
			validateSettings.error.run(option, option.prompts.error.badFormat);
		} else {
			if (!format2) {
				validateSettings.error.run(option,
						option.prompts.error.badLength);
			} else {
				if (!emailstate || emailold != option.value) {
					if (emailold != option.value) {
						emailold = option.value;
						option.errorEle
								.html("<span style='color:#999'>验证中...</span>");
						$.getJSON("../user/isEmailEngaged.action?email="
								+ escape(option.value) + "&r=" + Math.random(),
								function(date) {
									emailResult = date.success;
									if (date.success == "SUCCESS") {
										validateSettings.succeed.run(option);
										emailstate = true;
									}else {
										validateSettings.error.run(option,
												option.prompts.error.beUsed);
										emailstate = false;
									}
								})
					} else {
						if (emailResult != 2) {
							validateSettings.error.run(option,
									option.prompts.error.beUsed);
							emailstate = false;
						} else {
							validateSettings.error.run(option,
									option.prompts.error.beYahoo);
							emailstate = false;
						}

					}
				} else {
					validateSettings.succeed.run(option);
				}
			}
		}
	},
	authcode : function(option) {
		if (!authcodestate || authcodeold != option.value) {
			if (authcodeold != option.value) {
				authcodeold = option.value;
				option.errorEle.html("<span style='color:#999'>验证中...</span>");
				var token = $("#authcode").attr("value");
				$.ajax({
					url: "../user/verify.action?token=" + escape(option.value) + "&r=" + Math.random(),
					type: "GET",
					dataType: "json",
					success: function(data){
						if (data.success == "SUCCESS") {
							validateSettings.succeed.run(option);
							authcodestate = true;
						} else {
							validateSettings.error.run(option, option.prompts.error);
							authcodestate = false;
						}
					}
				})
				/*$.getJSON("../user/verify?token=" + escape(option.value)
						+ "&r=" + Math.random(), function(
						date) {
					if (date.success == "SUCCESS") {
						validateSettings.succeed.run(option);
						authcodestate = true;
					} else {
						validateSettings.error.run(option, option.prompts.error);
						authcodestate = false;
					}
				})*/
			} else {
				validateSettings.error.run(option, option.prompts.error);
				authcodestate = false;
			}
		} else {
			validateSettings.succeed.run(option);
		}
		//validateSettings.succeed.run(option);
		authcodestate = true;
	},
	protocol : function(option) {
		if (option.element.attr("checked") == true) {
			option.element.attr("sta", validateSettings.succeed.state);
			option.errorEle.html("");
		} else {
			option.element.attr("sta", validateSettings.isNull.state);
			option.succeedEle.removeClass(validateSettings.succeed.style);
		}
	},
	pwdstrength : function() {
		var element = $("#pwdstrength");
		var value = $("#pwd").val();
		if (value.length >= 6 && validateRules.isPwd(value)) {
			$("#pwd_error").empty();
			element.show();
			var level = pwdLevel(value);
			switch (level) {
			case 1:
				element.removeClass().addClass("strengthA");
				break;
			case 2:
				element.removeClass().addClass("strengthB");
				break;
			case 3:
				element.removeClass().addClass("strengthC");
				break;
			default:
				break;
			}
		} else {
			element.hide();
		}
	},
	checkGroup : function(elements) {
		for (var i = 0; i < elements.length; i++) {
			if (elements[i].checked) {
				return true;
			}
		}
		return false;
	},
	checkSelectGroup : function(elements) {
		for (var i = 0; i < elements.length; i++) {
			if (elements[i].value == -1) {
				return false;
			}
		}
		return true;
	},
	showPassword : function(type) {
		var v1 = $("#pwd").val(), s1 = $("#pwd").attr("sta"), c1 = document
				.getElementById("pwd").className, t1 = $("#pwd").attr(
				"tabindex");
		var v2 = $("#pwd2").val(), s2 = $("#pwd2").attr("sta"), c2 = document
				.getElementById("pwd2").className, t2 = $("#pwd2").attr(
				"tabindex");
		var P1 = $("<input type='" + type + "' value='" + v1 + "' sta='" + s1
				+ "' class='" + c1 + "' id='pwd' name='pwd' tabindex='" + t1
				+ "'/>");
		$("#pwd").after(P1).remove();
		$("#pwd").bind("keyup", function() {
			validateFunction.pwdstrength();
		}).jdValidate(validatePrompt.pwd, validateFunction.pwd)
		var P2 = $("<input type='" + type + "' value='" + v2 + "' sta='" + s2
				+ "' class='" + c2 + "' id='pwd2' name='pwd2' tabindex='" + t2
				+ "'/>");
		$("#pwd2").after(P2).remove();
		$("#pwd2").jdValidate(validatePrompt.pwd2, validateFunction.pwd2);
	},
	FORM_submit : function(elements) {
		var bool = true;
		for (var i = 0; i < elements.length; i++) {
			if ($(elements[i]).attr("sta") == 2) {
				bool = true;
			} else {
				bool = false;
				break;
			}
		}
		return bool;
	}
};
